package com.jm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jm.constants.SystemConstants;
import com.jm.domain.ResponseResult;
import com.jm.domain.entity.Comment;
import com.jm.domain.vo.CommentVo;
import com.jm.domain.vo.PageVo;
import com.jm.enums.AppHttpCodeEnum;
import com.jm.exception.SystemException;
import com.jm.mapper.CommentMapper;
import com.jm.mapper.UserMapper;
import com.jm.service.CommentService;
import com.jm.utils.BeanCopyUtils;
import com.jm.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-06-21 23:24:41
 */
@Service("commentService")
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult commentList(Long articleId, Integer pageNum, Integer pageSize) {
        log.info("articleId:{} ,pageNum:{} ，pageSize:{}",articleId, pageNum , pageSize);
        //查询条件
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //查询类型：文章/友联
        queryWrapper.eq(Comment::getType, articleId!=null?SystemConstants.ARTICLE_COMMENT:SystemConstants.LINK_COMMENT);
        //查询文章评论，才需要这个条件
        queryWrapper.eq(articleId!=null,Comment::getArticleId, articleId);
        //根评论id
        queryWrapper.eq(Comment::getRootId, SystemConstants.ROOT_ID);

        //分页查询
        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        List<Comment> records = page.getRecords();
        //把List<Comment>转换成List<CommentVo>
        List<CommentVo> commentVoList = toCommentVoList(records);
        //查询根评论的所有子评论
        for(CommentVo commentVo:commentVoList){
            List<CommentVo> children=getChildren(commentVo.getId());
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        try {
            Long userId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new RuntimeException("登陆后，才允许评论!!!");
        }
        //添加评论,内容不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    private List<CommentVo> getChildren(Long id) {
        //根据根评论的id查询所对应的子评论的集合
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        //评论按时间降序
        queryWrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> commentList = list(queryWrapper);
        List<CommentVo> commentVoList = toCommentVoList(commentList);
        return commentVoList;
    }

    //封装vo，并进行昵称赋值
    private List<CommentVo> toCommentVoList(List<Comment> list) {
        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        for (CommentVo commentVo : commentVoList) {
            //通过createBy查询用户的昵称并赋值
            String nickName = userMapper.selectById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才进行查询
            if(commentVo.getToCommentUserId()!=SystemConstants.ROOT_ID){
                String toCommentUserName = userMapper.selectById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVoList;
    }
}

